# Register your models here.
from django.contrib import admin
from django.contrib.auth.hashers import make_password

from project_manage.libs.core_auth.models import PersonModel

admin.site.empty_value_display = '-无数据-'
admin.site.site_title = 'BUG管理平台|By Hill'
admin.site.site_header = '项目BUG管理平台'


@admin.register(PersonModel)
class PersonAdmin(admin.ModelAdmin):
    list_display = ('account', 'name', 'role', 'is_active')
    fieldsets = (
        ('基本信息', {'fields': (('account', 'password',), ('name', 'phone',), ('role',),)}),
        ('状态', {'fields': (('is_superuser', 'is_active'),)}),
        ('权限', {'fields': ('groups', 'user_permissions')}),
    )
    filter_horizontal = ('groups', 'user_permissions',)

    def save_model(self, request, obj, form, change):
        if form.is_valid():
            password = form.data.get('password')
            if password and len(password) != 78:
                obj.password = make_password(password)
            super().save_model(request, obj, form, change)
